package org.onejava.core.manage;

import java.lang.reflect.Field;
import java.util.List;

import org.onejava.core.annotation.IBean;
import org.onejava.core.annotation.IService;
import org.onejava.core.bean.Allconfig;
import org.onejava.core.bean.ServiceEntity;
import org.onejava.core.bean.imp.Entity;
import org.onejava.core.bean.imp.IocPlugin;
import org.onejava.core.tools.ScanTool;

/**
 * 基础管理类
 * @author frost
 *
 */
public class BaseManage {
	
	/**
	 * 扫描出某目录下的文件
	 * @param packagePath
	 * @return
	 * @throws Exception
	 */
	protected List<String> scan(String packagePath) throws Exception {
		ScanTool sacntool = new ScanTool();
		List<String> list = sacntool.getAllClassName(Allconfig.truePath,packagePath);
		return list;
	}
	/**
	 * 为管理类提供Ioc支持
	 * @param entity
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	protected void setBeanAndService(Entity entity) throws Exception{
		if("".equals(Allconfig.springXml)||null==Allconfig.springXml){
			setService(entity);
		}
		PluginManage pm = PluginManage.getManage();
		IocPlugin ip =pm.getIocPlugin();
		if(null==ip){
			throw new Exception("没有找到可用的Ioc插件");
		}
		ServiceManage sm = ServiceManage.getMamage();
		Class<?> class1=entity.getObj().getClass();
		Field[] fieldList=class1.getFields();
		for (Field field : fieldList) {
			IBean spring=field.getAnnotation(IBean.class);
			IService iservice=field.getAnnotation(IService.class);

			if(spring!=null && iservice==null){
				Object bean=ip.getBean(field.getName());
				field.setAccessible(true);
				field.set(entity.getObj(), bean);
			}else if(spring==null && iservice!=null){
				ServiceEntity  service = sm.getService(field.getName());
				if(null==service){
					throw new Exception(class1+"中变量'"+field.getName()+"'无法找到与之对应的Service");
				}
				Object obj = service.getObj();
				field.setAccessible(true);
				field.set(entity.getObj(), obj);
			}else if(spring!=null && iservice!=null){
				throw new Exception(class1+"中变量'"+field.getName()+"'不能同时被spring和服务层注入");
			}
		}
		
	}
	
	/**
	 * 为管理类提供注入Bean支持
	 * @param entity
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	protected void setBean(Entity entity) throws Exception{
		if("".equals(Allconfig.springXml)||null==Allconfig.springXml){
			return;
		}
		PluginManage pm = PluginManage.getManage();
		IocPlugin ip =pm.getIocPlugin();
		if(null==ip){
			throw new Exception("没有找到可用的Ioc插件");
		}
		Class<?> class1=entity.getObj().getClass();
		Field[] fieldList=class1.getFields();
		for (Field field : fieldList) {
			IBean spring=field.getAnnotation(IBean.class);
			if(spring!=null){
				Object bean=ip.getBean(field.getName());
				field.setAccessible(true);
				field.set(entity.getObj(), bean);
			}
		}
	}
	
	/**
	 * 为管理类提供注入Service支持
	 * @param entity
	 * @throws Exception
	 */
	protected void setService(Entity entity) throws Exception{
		Class<?> class1=entity.getObj().getClass();
		Field[] fieldList=class1.getFields();
		for (Field field : fieldList) {
			IService iservice=field.getAnnotation(IService.class);
			if(iservice!=null){
				ServiceManage sm = ServiceManage.getMamage();
				ServiceEntity  service = sm.getService(field.getName());
				if(null==service){
					throw new Exception(class1+"中变量'"+field.getName()+"'无法找到与之对应的Service");
				}
				Object obj = service.getObj();
				field.setAccessible(true);
				field.set(entity.getObj(), obj);
			}
		}
	}
}
